<?php

class metacriticList extends metacritic
{
	protected $_platformsForList = array();
	
	function __construct(){
		parent::__construct();
	}
	
	public function parseTotalReleaseList() {
		$this->_content = self::__urlGetContent($this->_baseUrl);
		//file_put_contents ('./test/'.date('dmY').'list_real.html',$this->_content);
		//$this->_content = file_get_contents('./test/'.date('dmY').'list_real.html');
		
		//preg_match_all('/\<li class=\"product game_product(.*?)\"\>(.*?)([A-Za-z]{3} [0-9]+)\<\/span\>(.*?)\<\/ul\>/is',$this->_content,$products);
		preg_match_all('/\<li class=\"product game_product(.*?)\<\/li\>/is',$this->_content,$products);
		
		foreach ($products[1] as $index=>$sheet){
			preg_match('/[A-Z]{1}[a-z]{2}[ ]+[0-9]+/',$sheet,$release);
			$mcItem = new mcItem();
			$mcItem->setPlatform(self::__getMyPlatform($sheet));
			$sheet = explode('<br />',nl2br(strip_tags($sheet)));
			$mcItem->setTitle(self::__getMcTitle($sheet));
			$mcItem->setMcVote(self::__getMcVote($sheet));
			$mcItem->setUsVote(self::__getUsVote($sheet));
			$mcItem->setRelease($release[0]);
			$mcItem->setReleaseDate($release[0]);
			$this->_finalData[] = $mcItem;
		}
	}
	
	public function parseList(){
		//$this->_content = file_get_contents('./test/list_real.html');
		$finalUrl = str_replace ('[[platforms]]',$this->_platforms[$this->_plat],$this->_baseUrl);
		$this->_content = self::__urlGetContent($finalUrl);
		//file_put_contents ('./test/list_real.html',$this->_content);
		
		preg_match_all('/li class=\"product game_product(.*?)\"\>(.*?)([A-Za-z]{3} [0-9]+)\<\/span\>(.*?)\<\/li\>/is',$this->_content,$products);
		
		foreach ($products[2] as $index=>$sheet){
			preg_match('/data\"\>(.*?)\<\/span\>/is',$sheet,$releaseDate);
			$sheet = explode('<br />',nl2br(strip_tags($sheet)));
			$mcItem = new mcItem();
			$mcItem->setTitle(self::__getMcTitle($sheet));
			$mcItem->setMcVote(self::__getMcVote($sheet));
			$mcItem->setUsVote(self::__getUsVote($sheet));
			$mcItem->setRelease($products[3][$index]);
			$mcItem->setReleaseDate($products[3][$index]);
			$this->_finalData[] = $mcItem;
		}
	}
	
	private function __getMcTitle($list){
		foreach ($list as $index=>$data){
			if ( (trim($data) != "") && $index!=0) return trim($list[$index]);
		}
	}
	
	private function __getMcVote($list){
		foreach ($list as $index=>$data){
			if (trim($data) == "Metascore:") return trim($list[$index+1]);
		}
	}
	
	private function __getUsVote($list){
		foreach ($list as $index=>$data){
			if (trim($data) == "User:") return trim($list[$index+1]);
		}
	}
	
	public function renderXML(){
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
		<results>
		[[items]]
		</results>';
	
		$template = file_get_contents ('./template/releaseListItem.xml');
		$code = "";
		foreach ($this->_finalData as $plat=>$mcItem){
			$code .= $template;
			$code = str_replace ('[[title]]',$mcItem->getTitle(),$code);
			$code = str_replace ('[[mc_vote]]',$mcItem->getMcVote(),$code);
			$code = str_replace ('[[platform]]',$mcItem->getPlatform(),$code);
			$code = str_replace ('[[us_vote]]',$mcItem->getUsVote(),$code);
			$code = str_replace ('[[release]]',$mcItem->getRelease(),$code);
			$code = str_replace ('[[release_month]]',$mcItem->getReleaseMonth(),$code);
			$code = str_replace ('[[release_day]]',$mcItem->getReleaseDay(),$code);
		}
		$this->_response = str_replace ('[[items]]',$code,$xml);
		file_put_contents ('./store/release/'.date('Y-m-d').'_'.$this->_plat.'.xml',$this->_response);
		return $this->_response;
	}
	
}

?>